// pages/usercenter/usercenter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:{},
    list:[],
    url:[],
    windowHeight: 0,
    date:0,
    yeMo:'',
    maxday:0,
    year:0,
    month:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var app = getApp();
    var openid = app.globalData.openid;
    var that=this;
    var myDate = new Date();
    var mo = myDate.getMonth()+1;
    var ye = myDate.getFullYear();
    that.setData({
      openid: openid
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          date: myDate.getDate()-1,
          year:ye,
          month:mo,
          yeMo: ye + '.' + mo, //获取年月
          maxday: myDate.getDate() - 1,
        });
      }
    });
    var that=this;
    console.log(this.data.date)
    var date=this.data.date;
    if (date <= 9 && date >= 1) {
      date = '0' + date;
    } else if (date <= 0) {
      var dayCount = this.days(this.data.year, this.data.month - 1);//获取前一个月的天数
      var mo = this.data.month - 1;
      var ye = this.data.year;
      console.log("该月天数：" + dayCount)
      that.setData({
        date: dayCount,
        month: mo,
        yeMo: ye + '.' + mo, //获取年月
      })
      date = dayCount;
    }
    if (mo <= 9) {
      mo = '0' + mo;
    } 
    this.requestSite(that,date,mo);

  },
  requestSite: function (that,date,mo) {
    //显示加载动画
    wx.showLoading({
      title: '加载中',
      mask: false
    });
    var url= 'https://www.anyknew.com/past/2019/' + mo + '/' + date + '/p1';
    console.log(url)
    wx.showNavigationBarLoading();
    wx.request({
      url: 'https://www.anyknew.com/past/2019/'+mo+'/'+date+'/p1',
      
      method: 'get',
      success: function (res) { //请求成功
        //存数据
        
        
        // var reg = /<[^<>]+>/g   //1、全局匹配g肯定忘记写  2、<>标签中不能包含标签实现过滤HTML标签
        // var doc = res.data.replace(reg, '')
        // console.log(doc)
        // var pattern = /2019 /gi;
        // var match = pattern.exec(doc);
        // console.log(match)

        // var html = "<p>111</p> <p>222</p> <p>333</p>";
        // var resqq=html.match(/[^><]+(?=<\/p>)/img);
       



        // for(var t=result;t<=result+10;t++)
        // console.log(res.data[t])
        if(res.data!=404){
          console.log("没有404")
          var html = res.data;
          //链接正则
          var url = html.match(/go\/[^<]+(?=\n)/img);
          var resqq = html.match(/[^><]+(?=\n)/img);
          //var resqq = html.match(<h2[^<] *>\n < a[^<] * href=\"([^<]*?)\"[^<]*>(.*?)\n</a>\n</h2>\n);
          var back;
          //处理一下数据
          back = resqq.filter(function (item) {
            if (item.length >= 3 && item.length <= 50)
                 return item
          });
          // for(var t=0;t<resqq.length;t++){
          //   var indexb=0;
          //   if (resqq[t].length>=3&&resqq[t].length<=50) {
          //     back[indexb] = resqq[t];
          //     indexb++;
          //   }
          // }
          for(var t=0;t<url.length;t++){
            url[t]=url[t].substring(3,10);
          }

          console.log("话题："+back);
          console.log("链接：" + url);
          that.setData({
            list: back,
            url:url
          })
        }
        else{
          console.log("404！！！！！！！！！！")
          that.setData({
            list:[],
            [`list[0]`]: '该日榜单聚类中...'
          })
        }

      },
      fail: function () {
        wx.showToast({
          title: '服务器异常',
          image: '../../image/bad.png',
          duration: 1500
        })
      },
      complete: function () { //请求完成的时候，不管请求成功还是失败
        wx.hideLoading();
        wx.hideNavigationBarLoading()
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  tapleft: function (e) {
    console.log("向前一天", e)
    var that = this
    var date=this.data.date;
    date = date - 1;
    that.setData({
      date: date,
    })
    var mo = this.data.month;
    if (date <= 9 && date>=1){
      date='0'+date;
    } else if (date <= 0){
      var dayCount = this.days(this.data.year,this.data.month-1);//获取前一个月的天数
      mo=mo-1;
      var ye = this.data.year;
      console.log("该月天数："+dayCount)
      that.setData({
        date: dayCount,
        month: mo,
        yeMo: ye + '.' + mo, //获取年月
        maxday:dayCount+1
      })
      date=dayCount;
    }
    if (mo <= 9) {
      mo = '0' + mo;
    } 
    this.requestSite(that,date,mo);
  },
  tapright: function (e) {
    console.log("向后一天", e)
    var that = this
    var date = this.data.date;
    date=date+1;
    that.setData({
      date: date,
    })
    var dayCount = this.days(this.data.year, this.data.month);//获取这个月的天数
    var mo = this.data.month;
    if (date <= 9) {
      date = '0' + date;
    } else if (date >dayCount) {
      //var dayCount = this.days(this.data.year, this.data.month + 1);//获取后一个月的天数
      mo=mo+1;
      var ye = this.data.year;
      //console.log("该月天数：" + dayCount)
      that.setData({
        date: 1,
        month: mo,
        yeMo: ye + '.' + mo, //获取年月
      })
      date = '01';
      var myDate = new Date();
      if(mo==myDate.getMonth()+1){
        that.setData({
          maxday: myDate.getDate() - 1,
        });
      }
    }
    
    if (mo <= 9) {
      mo = '0' + mo;
    } 
    this.requestSite(that,date,mo);
  },
  days:function(year, month){
    var dayCount;
    var now = new Date(year, month, 0);
    dayCount = now.getDate();
    return dayCount;
  },
  //长按复制
  copy: function (e) {
    var that = this;
   // console.log(e);
    wx.setClipboardData({
      data: "https://www.anyknew.com/go/" + that.data.url[e.currentTarget.dataset.index],
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
    //console.log(this.data.list[e.currentTarget.dataset.aindex].iid[e.currentTarget.dataset.aindex]);
    //console.log(this.data.list[e.currentTarget.dataset.aindex].iid);
  },
})